package com.zhongchaowuye.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.zhongchaowuye.pojo.Car;
import com.zhongchaowuye.service.CarService;

@Controller
@RequestMapping("/car")
public class CarController {

	@Resource
	private CarService carService;
	
	@RequestMapping(value ="/getCar",method = RequestMethod.POST)
	@ResponseBody
	//根据carId获取Car对象信息
	public Map<String,Object> getCar(HttpServletRequest request,Car c){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			Car car=carService.getCar(c);
			reMap.put("car", car);
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", "ERROR");
		}
		return reMap;
	}

	@RequestMapping(value ="/getCarList",method = RequestMethod.POST)
	@ResponseBody
	//获取列表
	public Map<String,Object> getCarList(HttpServletRequest request,Car c){
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		try {
			//List<Map<String,Object>> rList=carService.getCarList(c); //不带分页（注意实现类中的相应写法）
			
			Page<Map<String,Object>> rList = (Page)carService.getCarList(c);
			
			//System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("pageIndex", c.getPageIndex());
			reMap.put("pageSize", c.getPageSize());
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", "ERROR");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/insertCar",method = RequestMethod.POST)
	@ResponseBody
	//添加
	public Map<String,Object> insertCar(HttpServletRequest request,Car c)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			int flag=carService.insertCar(c);
			//int flag = 0;
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateCar",method = RequestMethod.POST)
	@ResponseBody
	//修改
	public Map<String,Object> updateCar(HttpServletRequest request,Car c)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			int flag=carService.updateCar(c);
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/delCar",method = RequestMethod.POST)
	@ResponseBody
	//软删除
	public Map<String,Object> delCar(HttpServletRequest request,Car c)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		Map<String,Object> m=new HashMap<>();
		try 
		{
			int flag=carService.delCar(c);
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
}
